package org.xm.service.Impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xm.mapper.WarehouseMapper;
import org.xm.model.Item;
import org.xm.model.Order;
import org.xm.model.Warehouse;
import org.xm.service.WarehouseService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Autowired
    private WarehouseMapper warehouseMapper;

    @Override
    public List<Warehouse> getAllWarehouses() {
        return warehouseMapper.getAllWarehouses();
    }

    @Override
    public List<Item> getItemsByWarehouseId(Long warehouseId) {
        return warehouseMapper.getItemByWarehouseId(warehouseId);
    }

    @Override
    public List<Order> getAllOrders() {
        return warehouseMapper.getAllOrders();
    }

    @Override
    public void addItem(Item item) {
        // 1. 添加物品进入仓库
        warehouseMapper.addItem(item);
        log.info("[getAllOrders] itemId:{}", item.getItemId());
        // 2. 添加订单
        Order order = new Order();
        order.setDate(new Date(System.currentTimeMillis()));
        order.setQuantity(item.getQuantity());
        order.setWarehouseId(item.getWarehouseId());
        order.setItemId(item.getItemId());
        log.info("[getAllOrders] order:{}", order);
        warehouseMapper.addOrder(order);
        log.info("Quantity:{}", order.getQuantity());
        // 3. 修改库存金额
        warehouseMapper.addInventoryAmount(item.getQuantity() * item.getPlannedUnitPrice().intValue(), item.getWarehouseId());
    }

    @Override
    public void addWarehouse(Warehouse warehouse) {
        log.info("warehouse: {}", warehouse);
        warehouseMapper.addWarehouse(warehouse);
    }
}
